﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace HyperTerminalController
{
    class TakeScreenShots
    {
        
        IntPtr windowHandle;

        public TakeScreenShots(string processName)
        {
            //var proc1 = Process.GetProcesses();
            Process proc = Process.GetProcessesByName(processName).FirstOrDefault();

            if (proc == null || proc.MainWindowHandle == IntPtr.Zero)
                throw (new Exception("Process: " + processName + " not found"));
            
            windowHandle = proc.MainWindowHandle;
        
        }
        
// this code find the handle id of the hyperterminal window        
   
        /// <summary>
        /// Causes application to be foreground application
        /// </summary>
        public  void SetWindowForeGround()
        {
            //var proc = Process.GetProcessesByName("hypertrm").FirstOrDefault();
            //if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
            //    SetForegroundWindow(proc.MainWindowHandle);
            SetForegroundWindow(windowHandle);
            //pictureFileName = fileName;

            //System.Threading.Thread.Sleep(500);
            //TakePicture(proc.MainWindowHandle);
            
            
        }
        /// <summary>
        ///  Win 32 api to set window to foreground
        /// </summary>
        /// <param name="hwnd"></param>
        /// <returns>handle to window</returns>
        [DllImport("user32")]
        private static extern bool SetForegroundWindow(IntPtr hwnd);





        // gets dimension fo window
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        
        /// <summary>
        /// Given a handle it takes picture of frame
        /// </summary>
        /// <param name="handle"></param>
        public void  TakePicture(string pictureOutput)
        {
            Rectangle myRect = new Rectangle();
            //this code find size of window
            RECT rct;

            if (!GetWindowRect(windowHandle, out rct))
            {
                //throw (new Exception("Can't get window dimensions"));
                MessageBox.Show("ERROR");
            }
            else
            {
                //MessageBox.Show(rct.ToString());

                myRect.X = rct.Left;
                myRect.Y = rct.Top;
                myRect.Width = rct.Right - rct.Left;
                myRect.Height = rct.Bottom - rct.Top;

            }

            // this saves the picture by capturing only the hyperterminal window

            using (Bitmap bitmap = new Bitmap(myRect.Width, myRect.Height))
            {
                using(Graphics g = Graphics.FromImage(bitmap)){
                    SetWindowForeGround();
                    System.Threading.Thread.Sleep(400);
                    g.CopyFromScreen(new Point(myRect.X, myRect.Y), Point.Empty, new Size(myRect.Width, myRect.Height));
                    bitmap.Save(pictureOutput);
                    //return bitmap;
                }
            }



        }    
    
    
    
    
    
    
    
    
    
    }





}

